package org.ticketing.controller;

import javax.persistence.*;

import org.ticketing.model.*;

public class ReservationManager {

	// To be launched when reservation page is loaded
	public void checkAvailability(int eventId) {
		EntityManagerFactory factory = Persistence.createEntityManagerFactory("DB");
		EntityManager manager = factory.createEntityManager();
		
		// Manage concurrent accesses
		
		Query query = manager.createQuery("SELECT seat_1, seat_2, seat_3, "
				+ "seat_4, seat_5, seat_6 FROM reservations WHERE event='" 
					+ new Integer(eventId).toString() + "'");
		// List<Seat> = query.getResultList();
		
		// Disable already-occupied seats (click)
	}
	
	public void purchase(String username, int eventId, int[] seats) {
		EntityManagerFactory factory = Persistence.createEntityManagerFactory("DB");
		EntityManager manager = factory.createEntityManager();
		
		manager.getTransaction().begin();
		
		// manager.persist(new Reservation(username, eventId, seats));
			
		manager.getTransaction().commit();
	}

	public void sendMail(String username) {
		EntityManagerFactory factory = Persistence.createEntityManagerFactory("DB");
		EntityManager manager = factory.createEntityManager();
		
		User user = manager.find(User.class, username);
		// Send a mail to user's mail address
	}

}
